<?php
   // require_once('functions.php');

    //set up database variables
	$dbusername = "joshhughes22";
	$dbpass = "addisyn";
	$dbhost = "localhost";
	$database = "joshhughes_com_-_hockey";
	//Connect TO The Database
	mysql_connect($dbhost,$dbusername,$dbpass) or die("Cannot connect to the database for pick weight processing.<br/>");
	//Open Database
	mysql_select_db($database) or die("Cannot select the database for pick weight processing.<br/>");
	//Get Current Week
	$League = $L;
	//Get Current Week
	$Weeknum = $w;
	//UserID
	$userID = $func;
	
	// Get Number of games for this week.
	$GameCount = 0;
	$SQL = "SELECT COUNT(*) AS GameCount FROM ".$League."Schedule Where Week=".$Weeknum;
	$result = mysql_query($SQL) or die("Cannot query the database for pick weight processing.<br>");
	if($myrow = mysql_fetch_array($result))
	{
			$GameCount = $myrow["GameCount"];
	}
	
	
	//Get all the ranks for game that are already in progress or over.
	$query="SELECT GameNum, PickEmailed FROM ".$League."Schedule Where (Week=".$Weeknum." AND PickEmailed=1) ORDER BY GameNum";
	$doQuery=mysql_query($query);
	$numrows=mysql_num_rows($doQuery);
	$num = 1;
	if($numrows > 0)
	{
		while($results=mysql_fetch_array($doQuery))
		{
			$insidequery="SELECT Game".$results["GameNum"]."Rank FROM ".$League."WeighPicks WHERE (Week=".$Weeknum." AND UserID=".$userID.")";
			$doQueryinside=mysql_query($insidequery);
			$insidenumrows=mysql_num_rows($doQueryinside);
			if($insidenumrows > 0)
			{
				while($insideresults=mysql_fetch_array($doQueryinside))
				{
					$gameRanks[$num] = $insideresults["Game".$results["GameNum"]."Rank"];
					$num++;
				}
			}
		}
		
	}
	
		
	$ranking = $GameCount;
	#$ranking = count($pick_list);
	foreach ($pick_list as $GameNum)
	{
		while (in_array($ranking, $gameRanks))
			$ranking--;
			
		$SQL = "UPDATE ".$League."WeighPicks SET Game".$GameNum."Rank=".$ranking." WHERE (Week=$Weeknum AND UserID=$userID)";
		mysql_query($SQL) or die("Cannot query the database for pick weight updating.<br/>");
        $ranking--;
	}

?>